﻿using System;
using System.Data;

namespace org.se.fs.model.Table
{
    public class Branch : DataContext
    {
        public DataTable GetBranchLists()
        {
            using (var databaseManager = GetDatabase())
            {
                databaseManager.Open();
                var dataSet = databaseManager.ExecuteDataSet(CommandType.StoredProcedure, "GetBranchLists");
                return ToDataTable(dataSet);
            }
        }

        public DataTable GetBranchActive()
        {
            using (var databaseManager = GetDatabase())
            {
                databaseManager.Open();
                var dataSet = databaseManager.ExecuteDataSet(CommandType.StoredProcedure, "GetBranchActive");
                return ToDataTable(dataSet);
            }
        }

        public UInt64 Insert(string name, string code, string address, string email, string faxNum, string mobileNum, string telNum, UInt64 userId)
        {
            using (var databaseManager = GetDatabase())
            {
                databaseManager.Open();
                databaseManager.CreateParameters(8);
                databaseManager.AddParameters(0, "name", name);
                databaseManager.AddParameters(1, "code", code);
                databaseManager.AddParameters(2, "address",address );
                databaseManager.AddParameters(3, "email", email);
                databaseManager.AddParameters(4, "fax_num", faxNum);
                databaseManager.AddParameters(5, "mobile_num",mobileNum );
                databaseManager.AddParameters(6, "tel_num",telNum );
                databaseManager.AddParameters(7, "user_create_id", userId);
                var result = databaseManager.ExecuteScalar(CommandType.StoredProcedure, "InsertBranch");
                return Convert.ToUInt64(result);
            }
        }

        public bool Update(UInt64 id, string name, string code, string address, string email, string faxNum, string mobileNum, string telNum, UInt64 userId)
        {
            using (var databaseManager = GetDatabase())
            {
                databaseManager.Open();
                databaseManager.CreateParameters(9);
                databaseManager.AddParameters(0, "id", id);
                databaseManager.AddParameters(1, "name", name);
                databaseManager.AddParameters(2, "code", code);
                databaseManager.AddParameters(3, "address", address);
                databaseManager.AddParameters(4, "email", email);
                databaseManager.AddParameters(5, "fax_num", faxNum);
                databaseManager.AddParameters(6, "mobile_num", mobileNum);
                databaseManager.AddParameters(7, "tel_num", telNum);
                databaseManager.AddParameters(8, "user_create_id", userId);
                var result = databaseManager.ExecuteNonQuery(CommandType.StoredProcedure, "UpdateBranch");
                return Convert.ToBoolean(result);
            }
        }

        public bool Delete(UInt64 uid)
        {
            using (var databaseManager = GetDatabase())
            {
                databaseManager.Open();
                databaseManager.CreateParameters(1);
                databaseManager.AddParameters(0, "id", uid);
                var result = databaseManager.ExecuteNonQuery(CommandType.StoredProcedure, "DeleteBranch");
                return Convert.ToBoolean(result);
            }
        }
    }
}
